package sort.recharge.query;

import org.apache.commons.lang3.StringUtils;

import java.util.*;

public class QueryBanlenceTest {

    /**
     * 初始化有价书券枚举值
     */
    private final static Set<String> VALUABLE_BOOK_TIKET =
            new HashSet<String>(Arrays.asList("4", "5", "7", "8", "9", "10"));

    /**
     * 初始化无价书券枚举值
     */
    private final static Set<String> PRICELESS_BOOK_TIKET =
            new HashSet<String>(Arrays.asList("1", "2", "3", "6"));

    /**
     *判断rechargeType
     * @param inExtInfoMap
     * @return
     */
    public static String getRechargeType(Map<String, String> inExtInfoMap) {
        if (null == inExtInfoMap) {
            return null;
        } else {
            String ticketType = (String)inExtInfoMap.get("ticketType");
            if (StringUtils.isEmpty(ticketType)) {
                return null;
            } else {
                String rechargeType = null;
                if (!"0".equals(ticketType)) {
                    rechargeType = getRechargeTypeByTicketType(ticketType);
                } else {
                    String isPay = (String)inExtInfoMap.get("isPay");
                    if (!StringUtils.isEmpty(isPay)) {
                        rechargeType = getRechargeTypeByIsPay(isPay);
                    } else {
                        String realFee = (String)inExtInfoMap.get("realFee");
                        rechargeType = getRechargeTypeByRealFee(realFee);
                    }
                }

                return rechargeType;
            }
        }
    }

    /**
     * 根据ticketType判断rechargeType
     * @param ticketType
     * @return
     */
    private static String getRechargeTypeByTicketType(String ticketType)
    {
        String rechargeType = "";

        // 不在有价书券类型内
        if (!VALUABLE_BOOK_TIKET.contains(ticketType))
        {
            // 在无价书券类型内
            if (PRICELESS_BOOK_TIKET.contains(ticketType))
            {
                rechargeType = "0";
            }
        }
        else
        {
            rechargeType = "1";
        }

        return rechargeType;
    }


    /**
     * 根据isPay判断rechargeType
     * @param isPay
     * @return
     */
    private static String getRechargeTypeByIsPay(String isPay) {
        String rechargeType = null;
        if (null != isPay) {
            if ("1".equals(isPay)) {
                rechargeType = "1 充值";
            } else {
                rechargeType = "0 免费";
            }
        }

        return rechargeType;
    }


    /**
     * 根据费用判断rechargeType
     * @param realFee
     * @return
     */
    private static String getRechargeTypeByRealFee(String realFee) {
        String rechargeType = null;
        if (!StringUtils.isEmpty(realFee)) {
            int fee = Integer.parseInt(realFee);
            if (fee > 0) {
                rechargeType = "1 充值";
            } else if (0 == fee) {
                rechargeType = "0 免费";
            }
        }

        return rechargeType;
    }


}
